package com.android.execucaocampo;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.beans.Foto;
import com.android.beans.OrdemServico;
import com.android.beans.TempoExecucao;
import com.android.beans.TipoCorte;
import com.android.data.RepositorioFoto;
import com.android.data.RepositorioOrdemServico;
import com.android.data.RepositorioTempoExecucao;
import com.android.data.RepositorioTipoCorte;

public class FinalizarAtividadeCorteReligacaoActivity extends Activity {

	SimpleDateFormat out = new SimpleDateFormat("dd/MM/yyyy");
	
	EditText edLeitura;
	EditText edParecer;
	CheckBox chkRamalExposto;
	
	EditText edDescFt;
	String nome;
	
	ArrayList<TipoCorte> arrayTipoCorte;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.finalizaratividadecortereligacao);
		
		edLeitura = (EditText) findViewById(R.campo.leitura);
		edParecer = (EditText) findViewById(R.campo.parecer);
        chkRamalExposto = (CheckBox) findViewById(R.item.chkramalexposto);
		
		RepositorioTipoCorte repTipoCorte = new RepositorioTipoCorte(getApplication());
		arrayTipoCorte = repTipoCorte.listar("");
		
		final Spinner s2 = (Spinner)findViewById(R.drop.tipoCorte);
        ArrayAdapter<TipoCorte> adapter2 = new ArrayAdapter<TipoCorte>(this, android.R.layout.simple_spinner_item, arrayTipoCorte);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s2.setAdapter(adapter2);
	
        Button btnFinalizar = (Button) findViewById(R.botao.finalizar);
        btnFinalizar.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
            	if(s2.getSelectedItem().toString().equals("")){
                    Toast.makeText(FinalizarAtividadeCorteReligacaoActivity.this, "Falta informar o tipo do corte!!!", Toast.LENGTH_LONG).show();
            	} else if (edLeitura.getText().toString().equals("")) {
            		Toast.makeText(FinalizarAtividadeCorteReligacaoActivity.this, "Falta informar a leitura!!!", Toast.LENGTH_LONG).show();
            	} else {
            		RepositorioOrdemServico repOS = new RepositorioOrdemServico(getApplicationContext());
             		OrdemServico os = repOS.procurar(Integer.parseInt(AbaOrdemServicoActivity.numOS));
            		
             		if (os.getFotoInicial() == 1) {
             			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
						nome = "Foto Final.jpg";

						startActivityForResult(intent, 0);             		         				 				
             		} else {
             			repOS = new RepositorioOrdemServico(getApplicationContext());
                		os = repOS.procurar(Integer.parseInt(AbaOrdemServicoActivity.numOS));
                		
                		os.setDataFinalizacao(new Date());
                		os.setParecer(edParecer.getText().toString());
                		os.setLeitura(edLeitura.getText().toString());            		
                		os.setSituacao(3);
                		
                		os.setRamalExposto(chkRamalExposto.isChecked());
                		
                		TipoCorte tc = (TipoCorte)s2.getSelectedItem();
                		os.setCodigoTipoCorte(tc.getCodigo());
                		
                		RepositorioOrdemServico repOs = new RepositorioOrdemServico(getApplicationContext());
                		repOs.finalizarCorteAgua(os);
                		
                		RepositorioTempoExecucao repTempoExecucaoAux = new RepositorioTempoExecucao(getApplicationContext());						
						TempoExecucao te = repTempoExecucaoAux.listar(" WHERE horafim is null").get(0);
						repTempoExecucaoAux.atualizarHorario(te);
                		
                		Bundle b = new Bundle();		
                		b.putString("data", out.format(os.getDataProgramacao()));		
                		abrirEditar(b);
             		}
            	}
			}
		});
	}
	
	public void abrirEditar(Bundle extras){
    	Intent i = new Intent(this,ListaAtividadesActivity.class);
    	finish();
    	i.putExtras(extras);
    	startActivity(i);
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{	    		
		super.onActivityResult(requestCode, resultCode, data);     
		if (resultCode == RESULT_OK)  {  
			Bundle extras = data.getExtras();   
			Bitmap bmp = (Bitmap) extras.get("data");   
			
    		RepositorioFoto repFoto = new RepositorioFoto(getApplication());
    		Foto foto = new Foto();
    		foto.setCodigoordemservico(Integer.parseInt(AbaOrdemServicoActivity.numOS));
    		foto.setNome(nome);	   
			
			ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
	        bmp.compress(Bitmap.CompressFormat.PNG, 0, stream);  
	        foto.setFoto(stream.toByteArray());
	        repFoto.inserir(foto);
	        
	        RepositorioOrdemServico repOS = new RepositorioOrdemServico(getApplicationContext());
    		OrdemServico os = repOS.procurar(Integer.parseInt(AbaOrdemServicoActivity.numOS));
    		
    		os.setDataFinalizacao(new Date());
    		os.setParecer(edParecer.getText().toString());
    		os.setLeitura(edLeitura.getText().toString());            		
    		os.setSituacao(3);
    		
    		final Spinner s2 = (Spinner)findViewById(R.drop.tipoCorte);
    		TipoCorte tc = (TipoCorte)s2.getSelectedItem();
    		os.setCodigoTipoCorte(tc.getCodigo());
    		
    		RepositorioOrdemServico repOs = new RepositorioOrdemServico(getApplicationContext());
    		repOs.finalizarCorteAgua(os);
    		
    		RepositorioTempoExecucao repTempoExecucaoAux = new RepositorioTempoExecucao(getApplicationContext());						
			TempoExecucao te = repTempoExecucaoAux.listar(" WHERE horafim is null").get(0);
			repTempoExecucaoAux.atualizarHorario(te);
    		
    		Bundle b = new Bundle();		
    		b.putString("data", out.format(os.getDataProgramacao()));		
    		abrirEditar(b);
		}				    			    			    	    	    	
	}
}
